# -*- coding: utf-8 -*-

# -*- coding:utf-8 -*-
"""
合约价差趋势交易
"""
from jyinterface.interface import *
from interval3 import Interval as qj

class Straddle(JyInterface):
    symbol1 = 'BTC-USD-200925'
    symbol2 = 'BTC-USD-201225'
    longspread = 0.005      # 多远月，空近月，价差百分比
    shortspread = 0.02     # 多近月，空远月，价差百分比
    amount = 5             #传入的每次下单数量
    
    def __init__(self, **kwargs):
        super(Straddle,self).__init__(**kwargs)
        self.initiating()
        
    def initiating(self):
        """初始化策略"""
        self.l_spread =  [( self.longspread - i* 0.001) for i in range(5)] 
        self.s_spread = [(self.shortspread + i*0.001) for i in range(5)]
        self.order_amount =  [[i*-self.amount,i*self.amount] for i in range(1,6)] + [[i*self.amount,i*-self.amount] for i in range(1,6)]
        self.z = [qj(self.l_spread[1],self.l_spread[0],lower_closed=False),
             qj(self.l_spread[2],self.l_spread[1],lower_closed=False),
             qj(self.l_spread[3],self.l_spread[2],lower_closed=False),
             qj(self.l_spread[4],self.l_spread[3],lower_closed=False),
             qj.less_than(self.l_spread[4]),
             qj(self.s_spread[0],self.s_spread[1],upper_closed=False),
             qj(self.s_spread[1],self.s_spread[2],upper_closed=False),
             qj(self.s_spread[2],self.s_spread[3],upper_closed=False),
             qj(self.s_spread[3],self.s_spread[4],upper_closed=False),
             qj.greater_than(self.s_spread[4])
            ] 
        # self.s_amount = [[5,-5],[10,-10],[15,-15],[20,-20],[25,-25]]
        # self.l_amount = [[-5,5],[-10,10],[-15,15],[-20,20],[-25,25]]
        # self.ss = { k:v for k,v in zip(self.s_spread,self.s_amount) }
        # self.ll = { k:v for k,v in zip(self.l_spread,self.l_amount) }
    def trade(self,param):
        # 获取持仓,对比
        # 同时处理单个合约的多单、空单，要处理四次
        
        
        j_posotion = self.getPositionSt(self.symbol1)
        y_position = self.getPositionSt(self.symbol2)
        
        
        
    
        if param[0] - j_position[0] > 0:
            self.buy(self.symbol1,self.getDepth(self.symbol1).get('asks')[0][0],(param[0]-j_position[0])) 
        pass

    def loop(self):
        """策略部分"""
        try:
            jy = self.getDepth(self.symbol1).get('asks')[0][0]
            yy = self.getDepth(self.symbol2).get('bids')[0][0]
        except:
            print('获取价格失败')
        dif = (yy-jy)/jy
        xx = [i for i in range(len(self.z)) if dif in self.z[i] ]
        if xx :
            o_order = self.order_amount[xx[0]]
            self.trade(o_order)
        
        
       l_spread =  [(longspread - i* 0.001) for i in range(5)] 
       s_spread = [(shortspread + i*0.001) for i in range(5)]
        order_amount =  [[i*-amount,i*amount] for i in range(1,6)] + [[i*amount,i*-amount] for i in range(1,6)]
    
         order_amount[3]
        
        
        
        # 多近月空远月
        if self.l_spread[1] < dif <= self.l_spread[0] :
            l_amount = self.ll.get(self.l_spread[0])
            self.trade(l_amount)
        elif self.l_spread[2] < dif <= self.l_spread[1] :
            l_amount = self.ll.get(self.l_spread[1])
            self.trade(l_amount)
        elif self.l_spread[3] < dif <= self.l_spread[2] :
            l_amount = self.ll.get(self.l_spread[2])
            self.trade(l_amount)
        elif self.l_spread[4] < dif <= self.l_spread[3] :
            l_amount = self.ll.get(self.l_spread[3])
            self.trade(l_amount)
        elif  dif <= self.l_spread[4] :
            l_amount = self.ll.get(self.l_spread[4])
            self.trade(l_amount)
        # 多远月空近月
        elif self.s_spread[0] <= dif < self.s_spread[1] :
            s_amount = self.ss.get(self.s_spread[0])
            self.trade(s_amount)
        elif self.s_spread[1] <= dif < self.s_spread[2] :
            s_amount = self.ss.get(self.s_spread[1])
            self.trade(s_amount)
        elif self.s_spread[2] <= dif < self.s_spread[3] :
            s_amount = self.ss.get(self.s_spread[2])
            self.trade(s_amount)
        elif self.s_spread[3] <= dif < self.s_spread[4] :
            s_amount = self.ss.get(self.s_spread[3])
            self.trade(s_amount)
        elif self.s_spread[4] <= dif :
            s_amount = self.ss.get(self.s_spread[4])
            self.trade(s_amount)

if __name__ == "__main__":
    st = Straddle(type_=3,futureOrSpot=1,appKey='',secret='',passphrase='',transferId='M_3',grpcAddress="18.183.188.24:9309")
    st.run(st.loop)
